package cn.worken.baseframe.dubbo.facade.dto;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author genxin.liu
 * @since 2022/1/5 17:57
 */
@Data
@Builder
public class DResult<T> implements Serializable {
    public static String SUCCESS_CODE = "success";
    public static String FAIL_CODE = "fail";
    
    public static DResult SUCCESS_RESULT = success(SUCCESS_CODE);
    public static DResult FAIL_RESULT = fail(FAIL_CODE);
    
    /**
     * 结果码
     */
    private String code;
    /**
     * 信息
     */
    private String msg;
    /**
     * 数据
     */
    private T data;
    
    /**
     * 返回成功结果
     *
     * @author genxin.liu
     * @date 2022/1/6 9:39
     * @param:
     * @return DResult
     */
    public static DResult success() {
        return SUCCESS_RESULT;
    }
    
    /**
     * 返回成功信息
     *
     * @author genxin.liu
     * @date 2022/1/6 9:39
     * @param: msg
     * @return DResult
     */
    public static DResult success(String msg) {
        return DResult.builder().code(SUCCESS_CODE).msg(msg).build();
    }
    
    /**
     * 返回成功数据, data必须实现Serializable
     *
     * @author genxin.liu
     * @date 2022/1/6 9:38
     * @param: data 必须实现Serializable
     * @return DResult<T>
     */
    public static <T extends Serializable> DResult<T> success(T data) {
        return DResult.<T>builder().code(SUCCESS_CODE).msg(SUCCESS_CODE).data(data).build();
    }
    
    /**
     * 返回成功数据集合, data必须实现Serializable
     *
     * @author genxin.liu
     * @date 2022/1/6 9:38
     * @param: data 必须实现Serializable
     * @return DResult<T>
     */
    public static <T extends Serializable> DResult<List<T>> success(List<T> data) {
        return DResult.<List<T>>builder().code(SUCCESS_CODE).msg(SUCCESS_CODE).data(data).build();
    }
    
    /**
     * 返回失败结果
     *
     * @author genxin.liu
     * @date 2022/1/6 9:39
     * @param:
     * @return DResult
     */
    public static DResult fail() {
        return FAIL_RESULT;
    }
    
    /**
     * 返回失败信息
     *
     * @author genxin.liu
     * @date 2022/1/6 9:39
     * @param: msg
     * @return DResult
     */
    public static DResult fail(String msg) {
        return DResult.builder().code(FAIL_CODE).msg(msg).build();
    }
    
    /**
     * 返回失败数据，data必须实现Serializable
     *
     * @author genxin.liu
     * @date 2022/1/6 9:39
     * @param: data 必须实现Serializable
     * @return DResult<T>
     */
    public static <T extends Serializable> DResult<T> fail(T data) {
        return DResult.<T>builder().code(FAIL_CODE).msg(FAIL_CODE).data(data).build();
    }
    
    /**
     * 返回失败数据集合，data必须实现Serializable
     * @param data
     * @param <T>
     * @return
     */
    public static <T extends Serializable> DResult<List<T>> fail(List<T> data) {
        return DResult.<List<T>>builder().code(FAIL_CODE).msg(FAIL_CODE).data(data).build();
    }
    
    /**
     * 返回失败信息和数据，data必须实现Serializable
     *
     * @author genxin.liu
     * @date 2022/1/6 9:39
     * @param: msg
     * @param: data  必须实现Serializable
     * @return DResult<T>
     */
    public static <T extends Serializable> DResult<T> fail(String msg, T data) {
        return DResult.<T>builder().code(FAIL_CODE).msg(msg).data(data).build();
    }
    
    /**
     * 返回失败信息和数据集合，data必须实现Serializable
     *
     * @author genxin.liu
     * @date 2022/1/6 10:24
     * @param: msg
     * @param: data
     * @return DResult<java.util.List < T>>
     */
    public static <T extends Serializable> DResult<List<T>> fail(String msg, List<T> data) {
        return DResult.<List<T>>builder().code(FAIL_CODE).msg(msg).data(data).build();
    }
    
    /**
     * 判断返回结果是否成功
     *
     * @author genxin.liu
     * @date 2022/1/12 9:56
     * @param:
     * @return boolean
     */
    public boolean isSuccess() {
        return SUCCESS_CODE.equals(this.code);
    }
}
